﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel;
using Model;
using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System.Text;

namespace Infrastructure.Repository.Sys
{
    /// <summary>
    /// 公司仓储接口实现
    /// </summary>
    public class SysCorporationRepository : Repository<SysCorporation>, ISysCorporationRepository
    {
        /// <summary>
        /// 获取所有公司数据
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="authWhere">权限条件</param>
        /// <returns></returns>
        public List<SysCorporationOutPut> GetAllCorporation(string? corporationKey, string? authWhere)
        {
            StringBuilder sql = new(@" select sc.*,su.UserName from  Sys_Corporation as  sc 
                            inner join Sys_User as su on sc.CreateUser = su.UserId ");
            var qp = new QueryParameter();
            if (!string.IsNullOrWhiteSpace(corporationKey) && Const.XC_CorporationKey != corporationKey)
            {
                qp.listWhere.Append(" and sc.CorporationKey = @CorporationKey ");
                qp.dynamicParameter.Add("@CorporationKey", corporationKey);
            }
            if (!string.IsNullOrWhiteSpace(authWhere))
            {
                qp.listWhere.Append(authWhere);
            }
            if (!string.IsNullOrEmpty(qp.listWhere.ToString()))
            {
                sql.Append(" WHERE 1=1 " + qp.listWhere.ToString());
            }
            sql.Append("  order by sc.CreateTime desc");
           
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysCorporationOutPut>(sql.ToString(), qp.dynamicParameter).ToList();
        }

        /// <summary>
        /// 获取公司下的系统
        /// </summary>
        /// <returns></returns>
        public List<TreeOutPut> GetCorporationSystemList()
        {
            string sql = @" select CONVERT(VARCHAR(36), SystemKey) as [Key],CONVERT(VARCHAR(36), CorporationKey) AS [PKey]  ,SystemName as  [Value],CASE
                                    WHEN IsOpen = 1 THEN 0
                                    ELSE IsOpen
                                    END AS Disabled from  Sys_System 
                                    UNION 
                                    SELECT CONVERT(VARCHAR(36), CorporationKey)  as [Key], '' as  [PKey] , CorporationName as  [Value] ,CASE
                                    WHEN IsOpen = 1 THEN 0
                                    ELSE IsOpen
                                    END AS Disabled from Sys_Corporation";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<TreeOutPut>(sql).ToList();
        }

        /// <summary>
        /// 根据公司名称和编码，获取公司数据
        /// </summary>
        /// <param name="name">公司名称</param>
        /// <param name="code">公司编码</param>
        /// <returns>返回指定数据</returns>
        public List<SysCorporationOutPut> GetCorporationMsg(string? name, string? code)
        {
            string sql = @" select * from Sys_Corporation where CorporationName = @CorporationName OR CorporationCode=@CorporationCode ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysCorporationOutPut>(sql, new { CorporationName = name, CorporationCode = code }).ToList();
        }

        /// <summary>
        /// 根据编码，获取公司数据
        /// </summary>
        /// <param name="code">公司编码</param>
        /// <returns>返回指定数据</returns>
        public SysCorporationOutPut GetCorporationByCodeMsg(string code)
        {
            string sql = @" select * from Sys_Corporation where  CorporationCode=@CorporationCode ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.QueryFirst<SysCorporationOutPut>(sql, new { CorporationCode = code });
        }

        /// <summary>
        /// 设置公司是否启用
        /// </summary>
        /// <param name="corporationKeys">公司key集合</param>
        /// <param name="isOpen">是否启用</param>
        public void SetCorporationIsOpen(List<string> corporationKeys, bool isOpen)
        {
            string sql = "   update Sys_Corporation set IsOpen=@IsOpen where CorporationKey in @CorporationKey  ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Query(sql, new { CorporationKey = corporationKeys, IsOpen = isOpen });
        }
    }
}
